package com.example.project.controller;

import com.example.project.common.properties.JwtProperties;
import com.example.project.common.result.Result;
import com.example.project.common.utils.JwtUtil;
import com.example.project.pojo.entity.User;
import com.example.project.pojo.vo.UserLoginVO;
import com.example.project.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result<UserLoginVO>login(String code){
        log.info("微信登录code：{}",code);
        User user=userService.wxLogin(code);

        //为微信用户生成jwt
        Map<String,Object>claims=new HashMap<>();
        claims.put("userId",user.getUid());
        String token=JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(),claims);

        UserLoginVO userLoginVO=UserLoginVO.builder()
                .id(user.getUid())
                .openid(user.getOpenid())
                .token(token)
                .build();
        return Result.success(userLoginVO);

    }


}
